<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>导入浏览器数据</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            background: #f8f9fa;
            min-height: 100vh;
            padding: 20px;
            color: #333;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
        }

        header {
            text-align: center;
            margin-bottom: 40px;
            color: #333;
        }

        h1 {
            font-size: 2.2rem;
            margin-bottom: 10px;
            font-weight: 600;
            color: #2c3e50;
        }

        .actions {
            display: flex;
            justify-content: center;
            gap: 20px;
            margin-bottom: 30px;
        }

        .btn {
            display: inline-block;
            padding: 10px 20px;
            background: #007bff;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            font-weight: 500;
            transition: background-color 0.2s ease;
            border: none;
            cursor: pointer;
            font-size: 14px;
        }

        .btn:hover {
            background: #0056b3;
        }

        .card {
            background: white;
            border: 1px solid #e9ecef;
            border-radius: 6px;
            padding: 20px;
            margin-bottom: 20px;
        }

        h2 {
            color: #555;
            margin-bottom: 20px;
            padding-bottom: 10px;
            border-bottom: 2px solid #667eea;
        }

        h3 {
            color: #666;
            margin-bottom: 15px;
        }

        .import-section {
            margin-bottom: 40px;
            padding: 25px;
            border: 1px solid #e1e8ed;
            border-radius: 10px;
            background: #f8f9fa;
        }

        .instructions {
            background: #e3f2fd;
            padding: 20px;
            border-radius: 8px;
            margin-bottom: 25px;
        }

        .instructions ul {
            padding-left: 20px;
            margin: 15px 0;
        }

        .instructions li {
            margin-bottom: 10px;
            line-height: 1.6;
        }

        .instructions strong {
            color: #1976d2;
        }

        .form-group {
            margin-bottom: 20px;
        }

        label {
            display: block;
            margin-bottom: 8px;
            font-weight: 600;
            color: #444;
        }

        input[type="file"] {
            width: 100%;
            padding: 12px;
            border: 2px dashed #ccc;
            border-radius: 8px;
            background: white;
            transition: border-color 0.3s ease;
        }

        input[type="file"]:hover {
            border-color: #667eea;
        }

        input[type="file"]::-webkit-file-upload-button {
            background: #667eea;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-weight: 600;
            transition: background 0.3s ease;
        }

        input[type="file"]::-webkit-file-upload-button:hover {
            background: #5a67d8;
        }

        .btn {
            display: inline-block;
            padding: 12px 30px;
            background: #4CAF50;
            color: white;
            text-decoration: none;
            border-radius: 8px;
            font-weight: 600;
            border: none;
            cursor: pointer;
            font-size: 1rem;
            transition: all 0.3s ease;
            width: 100%;
        }

        .btn:hover {
            background: #45a049;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(0,0,0,0.2);
        }

        .btn-secondary {
            background: #6c757d;
        }

        .btn-secondary:hover {
            background: #5a6268;
        }

        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 8px;
            text-align: center;
            display: none;
        }

        .result.success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .result.error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        @media (max-width: 768px) {
            .container {
                padding: 20px;
                margin: 10px;
            }
            
            h1 {
                font-size: 1.5rem;
            }
            
            .import-section {
                padding: 15px;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <header>
            <h1>📥 导入浏览器数据</h1>
        </header>

        <!-- 操作按钮 -->
        <div class="actions">
            <a href="/" class="btn">🏠 返回首页</a>
            <a href="/test-extension" class="btn">🧪 扩展测试</a>
            <a href="/stat" class="btn">📊 统计数据</a>
            <a href="/ai-grouping" class="btn">🤖 AI智能分组</a>
        </div>
        
        <!-- 浏览历史导入 -->
        <div class="import-section">
            <h2>导入浏览历史</h2>
            <div class="instructions">
                <h3>如何导出浏览器历史记录：</h3>
                <ul>
                    <li><strong>Chrome浏览器：</strong>进入历史记录页面，使用扩展程序或开发者工具导出为JSON格式</li>
                    <li><strong>Firefox浏览器：</strong>通过about:downloads页面或使用插件导出</li>
                    <li><strong>Edge浏览器：</strong>设置 > 隐私、搜索和服务 > 服务 > 查看Microsoft Edge中的活动历史记录</li>
                </ul>
                <p>请确保导出的文件格式为JSON，并包含以下字段：url, title, visitTime, domain, path</p>
            </div>
            
            <form id="historyForm" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="historyFile">选择浏览历史文件 (JSON格式)：</label>
                    <input type="file" id="historyFile" name="file" accept=".json" required>
                </div>
                <button type="submit" class="btn">导入浏览历史</button>
            </form>
            
            <div id="historyResult" class="result"></div>
        </div>
        
        <!-- 书签导入 -->
        <div class="import-section">
            <h2>导入书签</h2>
            <div class="instructions">
                <h3>如何导出浏览器书签：</h3>
                <ul>
                    <li><strong>Chrome浏览器：</strong>书签 > 导出书签到HTML文件</li>
                    <li><strong>Firefox浏览器：</strong>书签 > 管理所有书签 > 导入和备份 > 导出书签到HTML</li>
                    <li><strong>Edge浏览器：</strong>收藏夹 > 导出收藏夹</li>
                </ul>
                <p>请确保导出的文件格式为JSON，并包含以下字段：id, title, url, dateAdded, parentId</p>
            </div>
            
            <form id="bookmarkForm" enctype="multipart/form-data">
                <div class="form-group">
                    <label for="bookmarkFile">选择书签文件 (JSON格式)：</label>
                    <input type="file" id="bookmarkFile" name="file" accept=".json" required>
                </div>
                <button type="submit" class="btn">导入书签</button>
            </form>
            
            <div id="bookmarkResult" class="result"></div>
        </div>
        
        <div style="text-align: center; margin-top: 30px;">
            <a href="/" class="btn btn-secondary">返回导航页面</a>
        </div>
    </div>

    <script>
        // 导入浏览历史
        document.getElementById('historyForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const formData = new FormData();
            const fileInput = document.getElementById('historyFile');
            const resultDiv = document.getElementById('historyResult');
            
            if (!fileInput.files[0]) {
                showResult(resultDiv, '请先选择文件', 'error');
                return;
            }
            
            formData.append('file', fileInput.files[0]);
            
            fetch('/api/import-history', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 200) {
                    showResult(resultDiv, data.message, 'success');
                    // 清空文件输入
                    fileInput.value = '';
                } else {
                    showResult(resultDiv, data.message, 'error');
                }
            })
            .catch(error => {
                showResult(resultDiv, '导入失败: ' + error.message, 'error');
            });
        });
        
        // 导入书签
        document.getElementById('bookmarkForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const formData = new FormData();
            const fileInput = document.getElementById('bookmarkFile');
            const resultDiv = document.getElementById('bookmarkResult');
            
            if (!fileInput.files[0]) {
                showResult(resultDiv, '请先选择文件', 'error');
                return;
            }
            
            formData.append('file', fileInput.files[0]);
            
            fetch('/api/bookmarks', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.code === 200) {
                    showResult(resultDiv, data.message, 'success');
                    // 清空文件输入
                    fileInput.value = '';
                } else {
                    showResult(resultDiv, data.message, 'error');
                }
            })
            .catch(error => {
                showResult(resultDiv, '导入失败: ' + error.message, 'error');
            });
        });
        
        // 显示结果
        function showResult(element, message, type) {
            element.textContent = message;
            element.className = 'result ' + type;
            element.style.display = 'block';
            
            // 3秒后自动隐藏成功消息
            if (type === 'success') {
                setTimeout(() => {
                    element.style.display = 'none';
                }, 3000);
            }
        }
    </script>
</body>
</html>